package com.etrans.web.gps.business.InfoCenterTask.timer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONException;
import com.etrans.web.gps.business.InfoCenterTask.service.InfoCenterService;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.httpclient.HTTPException;
import com.etrans.web.gps.util.tencentsms.SmsMultiSender;
import com.etrans.web.gps.util.tencentsms.SmsMultiSenderResult;

/**
 * 
 * @author zhangw
 *
 */
public class InfoCenterTask {

	private static final Logger logger = LoggerFactory.getLogger(InfoCenterTask.class);

	@Autowired
	private Config config;
	
	@Autowired
	private InfoCenterService infoCenter;

	/**
	 * 每天定时短信发送
	 */
	public void sendSMSDay() {
		// 指定模板ID单发短信
        try {
        	Date msgTime = DateUtil.getPreviousDayDate();
        	//当前发送时间
			List<String> phoneNumbers = infoCenter.queryInfoCenter();
			logger.info("南宁信息中心定时短信通知启动:" + msgTime + "===发送人数:" + phoneNumbers.size());
			List<String> params = infoCenter.getSendFormat(msgTime);
            SmsMultiSender msender = new SmsMultiSender(config.app_id, config.app_key);
            SmsMultiSenderResult result =  msender.sendWithParam("86", new ArrayList<String>(phoneNumbers),
            	config.template_id, new ArrayList<String>(params), "", "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            logger.info(result.toString());
            logger.info("南宁信息中心定时短信通知完成:历时" + (System.currentTimeMillis() - msgTime.getTime())/1000 + "秒");
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
            logger.error(e.getMessage());
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
            logger.error(e.getMessage());
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
            logger.error(e.getMessage());
        }
	}
}
